###########################################################################
#                            TextMate Snippets                            #
###########################################################################

# ==============
# Snippets 
# ==============
priority -50

snippet #ifndef "#ifndef ... #define ... #endif"
#ifndef ${1/([A-Za-z0-9_]+).*/$1/}
#define ${1:SYMBOL} ${2:value}
#endif /* ifndef $1 */
endsnippet

snippet #if "#if #endif" b
#if ${1:0}
${VISUAL}$0
#endif
endsnippet

snippet mark "#pragma mark (mark)"
#if 0
${1:#pragma mark -
}#pragma mark $2
#endif

$0
endsnippet

snippet main "main() (main)"
int main(int argc, char *argv[])
{
	${VISUAL}$0
	return 0;
}
endsnippet

snippet once "Include header once only guard"
#ifndef ${1:`!p
if not snip.c:
	import random, string
	name = re.sub(r'[^A-Za-z0-9]+','_', snip.fn).upper()
	rand = ''.join(random.sample(string.ascii_letters+string.digits, 8))
	snip.rv = ('%s_%s' % (name,rand)).upper()
else:
	snip.rv = snip.c`}
#define $1

${VISUAL}$0

#endif /* end of include guard: $1 */
endsnippet

snippet fprintf "fprintf ..."
fprintf(${1:stderr}, "${2:%s}\n"${2/([^%]|%%)*(%.)?.*/(?2:, :\);)/}$3${2/([^%]|%%)*(%.)?.*/(?2:\);)/}
endsnippet


snippet st "struct"
struct ${1:`!p snip.rv = (snip.basename or "name") + "_t"`} {
	${0:/* data */}
};
endsnippet

snippet fun "function" b
${1:void} ${2:function_name}($3)
{
	${VISUAL}$0
}
endsnippet

snippet fund "function declaration" b
${1:void} ${2:function_name}($3);
endsnippet

snippet head "File Header" b
/******************************************************************************
* File:             `!p snip.rv = fn`
*
* Author:           ${2}  
* Created:          `date +%m/%d/%y` 
* Description:      ${4:${VISUAL}}
*****************************************************************************/
${0}
endsnippet

post_jump "if snip.tabstop == 0 : split_line(snip.tabstops[4].current_text)"
snippet func "Function Header" 
/******************************************************************************
* Function:         $1
* Description:      ${4:${VISUAL}}
* Where:`!p 
snip.rv = ""
snip >> 2

args = get_args(t[2])
if args:
	for arg in args:
		snip.rv += '\n' + '*' + ' '*19 + arg + ' - TODO'
snip << 2
`
* Return:           $5
* Error:            $6
*****************************************************************************/
${1}($2){
	${0}
}
endsnippet
# custom snippets begin
snippet '(\S+)\*\*([\d.]+)([^\d.])' "exponentiation, need #include <math.h>" wrA
pow(`!p snip.rv=match.group(1)`,`!p snip.rv=match.group(2)`)`!p snip.rv=match.group(3)`$0
endsnippet
snippet i "include header files" b
#include $0
endsnippet
snippet def "define constants" b
#define $0
endsnippet

snippet .. "->" iA
->
endsnippet
snippet p "print something"
printf("$1\n"$2);$0
endsnippet

snippet pri "printf(..)" b
printf("$1"${2/..*/, /}$2);
endsnippet

snippet pln "printf(..\n)" b
printf("$1\n"${2/..*/, /}$2);
endsnippet

snippet s "scanf something"
scanf("$1",&$2);$0
endsnippet
snippet while "while statement"
while (${1:condition}) {
	$0
}
endsnippet
snippet if "if statement"
if (${1:condition}) {
	$0
}
endsnippet
snippet elif "else if"
else if (${1:condition}) {
	$0
}
endsnippet
snippet else "else"
else {
	$0
}
endsnippet
snippet for "for-loop"
for (${1:var}; ${2:condition}; ${3:update}) {
	$0
}
endsnippet
snippet switch "switch statement"
switch (${1:expression}) {
case ${2:condition}:
	${3:block}
	break;
${0:<<>>}
}
endsnippet
snippet case "case in switch statement"
case ${1:condition}:
	${2:block}
	break;
${0:<<>>}
endsnippet
# custom snippets end
